package com.blacktech.dbu.core.exception;

import com.blacktech.dbu.core.response.ResponseCode;

/**
 * 业务异常基类
 *
 * @author Yanyunsong
 */
public class BusinessException extends RuntimeException {

    /**
     * 错误码
     */
    private final Integer code;

    /**
     * 错误消息
     */
    private final String message;

    /**
     * 异常数据（可选，用于传递额外信息）
     */
    private final Object data;

    public BusinessException(ResponseCode responseCode) {
        super(responseCode.getMessage());
        this.code = responseCode.getCode();
        this.message = responseCode.getMessage();
        this.data = null;
    }

    public BusinessException(ResponseCode responseCode, String message) {
        super(message);
        this.code = responseCode.getCode();
        this.message = message;
        this.data = null;
    }

    public BusinessException(ResponseCode responseCode, Object data) {
        super(responseCode.getMessage());
        this.code = responseCode.getCode();
        this.message = responseCode.getMessage();
        this.data = data;
    }

    public BusinessException(ResponseCode responseCode, String message, Object data) {
        super(message);
        this.code = responseCode.getCode();
        this.message = message;
        this.data = data;
    }

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public BusinessException(Integer code, String message, Object data) {
        super(message);
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
        this.data = null;
    }

    /**
     * 创建用户不存在异常
     */
    public static BusinessException userNotFound() {
        return new BusinessException(ResponseCode.USER_NOT_FOUND);
    }

    /**
     * 创建用户名已存在异常
     */
    public static BusinessException usernameAlreadyExists() {
        return new BusinessException(ResponseCode.USERNAME_ALREADY_EXISTS);
    }

    /**
     * 创建密码错误异常
     */
    public static BusinessException passwordIncorrect() {
        return new BusinessException(ResponseCode.PASSWORD_INCORRECT);
    }

    /**
     * 创建用户已禁用异常
     */
    public static BusinessException userDisabled() {
        return new BusinessException(ResponseCode.USER_DISABLED);
    }

    /**
     * 创建产品不存在异常
     */
    public static BusinessException productNotFound() {
        return new BusinessException(ResponseCode.PRODUCT_NOT_FOUND);
    }

    /**
     * 创建产品代号已存在异常
     */
    public static BusinessException productCodeAlreadyExists() {
        return new BusinessException(ResponseCode.PRODUCT_CODE_ALREADY_EXISTS);
    }

    /**
     * 创建权限不足异常
     */
    public static BusinessException permissionDenied() {
        return new BusinessException(ResponseCode.PERMISSION_DENIED);
    }

    /**
     * 创建产品访问权限不足异常
     */
    public static BusinessException productAccessDenied() {
        return new BusinessException(ResponseCode.PRODUCT_ACCESS_DENIED);
    }

    /**
     * 创建数据库模式不存在异常
     */
    public static BusinessException schemaNotFound() {
        return new BusinessException(ResponseCode.SCHEMA_NOT_FOUND);
    }

    /**
     * 创建数据表不存在异常
     */
    public static BusinessException tableNotFound() {
        return new BusinessException(ResponseCode.TABLE_NOT_FOUND);
    }

    /**
     * 创建字段不存在异常
     */
    public static BusinessException columnNotFound() {
        return new BusinessException(ResponseCode.COLUMN_NOT_FOUND);
    }

    /**
     * 创建字段库记录不存在异常
     */
    public static BusinessException fieldLibraryNotFound() {
        return new BusinessException(ResponseCode.FIELD_LIBRARY_NOT_FOUND);
    }

    /**
     * 创建字段已存在异常
     */
    public static BusinessException fieldAlreadyExists() {
        return new BusinessException(ResponseCode.FIELD_ALREADY_EXISTS);
    }

    /**
     * 创建字段映射已存在异常
     */
    public static BusinessException fieldMappingAlreadyExists() {
        return new BusinessException(ResponseCode.FIELD_MAPPING_ALREADY_EXISTS);
    }

    // Getters
    public Integer getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public Object getData() {
        return data;
    }

    @Override
    public String toString() {
        return "BusinessException{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}